<?php

App::uses('Crud', 'Model');
App::uses('UserArea', 'Model');
App::uses('Areas', 'Model');
App::uses('Lines', 'Model');
App::uses('Workstations', 'Model');

class Training extends Crud
{
    /**
     * Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    const TRAINING = 1;
    const ABOUT_EXPIRE = 7;
    public $useTable = 'certifications';

    /**
     * Buscamos una por su nombre
     * @param string $name
     * @return array
     */
    public function getByName($name) {
        return $this->find('first', array('conditions' => array(
                        'name' => $name
        )));
    }

    /**
     * Obtenemos los registros para el grid
     * @return array
     */
    public function getEnabled($uid = 0, $lineId) {
        $filters = array(
            'fields' => array(
                'Training.id as value',
                'workstations.description as workstation',
                'lines.name as line',
                'Training.start as start',
                'Training.end as end',
                'Training.life as life',
                'DATEDIFF(Training.end,CURDATE()) < workstations.notification_end as proximos',
                'Training.workstation_id as ws_id',
                'count(links.id) as dependences'
            ),
            'joins' => array(
                array(
                    'table' => 'workstations',
                    'type' => 'inner',
                    'conditions' => array('Training.workstation_id = workstations.id')
                ),
                array(
                    'table' => 'lines',
                    'type' => 'inner',
                    'conditions' => array('workstations.line_id = lines.id')
                ),
                array(
                    'table' => 'workstations_configs',
                    'type' => 'inner',
                    'conditions' => array('workstations_configs.workstation_id = workstations.id')
                ),
                array(
                    'table' => 'links',
                    'type' => 'left',
                    'conditions' => array('links.source = workstations_configs.id')
                )

            ),
            'conditions' => array(
                'Training.user_id' => $uid,
                'Training.status' => self::STATUS_ENABLED,
                'Training.training' => self::TRAINING
            ),
            'order' => array(
                'end ASC'
            ),
            'group' => array(
                'Training.id',
                'workstations.description',
                'lines.name',
                'Training.start',
                'Training.end',
                'Training.life',
                'Training.end',
                'Training.workstation_id'
            )
        );
        if ( $lineId != null ) {
            $filters['conditions']['lines.id'] = $lineId;
        }
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    function getEnabledNextToEndByUser($userId) {
        $filters = array(
            'fields' => array(
                'Training.id as value',
                'a.name as area',
                'workstations.description as workstation',
                'lines.name as line',
                'Training.end as end',
                'DATE_ADD(Training.start, INTERVAL workstations.days DAY ) as min_end',
                'DATE_ADD(Training.start, INTERVAL workstations.max_days DAY ) as max_end',
                'u.nomina as nomina',
                'u.full_name as name'
            ),
            'joins' => array(
                array(
                    'table' => 'user_areas',
                    'alias' => 'ua',
                    'type' => 'inner',
                    'conditions' => array(
                        'ua.user_id' => $userId,
                        'ua.status' => UserArea::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'inner',
                    'conditions' => array(
                        'u.id = Training.user_id',
                        'u.status' => User::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'inner',
                    'conditions' => array(
                        'a.id = ua.area_id',
                        'a.status' => Area::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'lines',
                    'type' => 'inner',
                    'conditions' => array(
                        'lines.area_id = a.id'
                    )
                ),
                array(
                    'table' => 'workstations',
                    'type' => 'inner',
                    'conditions' => array(
                        'workstations.line_id = lines.id',
                        'workstations.status' => Workstation::STATUS_ENABLED
                    )
                ),
            ),
            'conditions' => array(
                'Training.workstation_id = workstations.id',
                'DATEDIFF(Training.end,CURDATE()) < workstations.notification_end',
                'Training.status' => self::STATUS_ENABLED,
                'Training.training' => self::TRAINING
            ),
            'order' => array(
                'area ASC',
                'line ASC',
                'workstation ASC',
                'end ASC',
            )
        );

        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

}
